package pms.dao;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.Event;
import pms.om.Kunde;
import pms.om.Theater;

/**
 * The Data access class for boats. All Interaction with the database
 * regarding the entity bean Boot should be handled by this class!
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class KundeDao extends HibernateDaoSupport {
	private KundeDao kundeDao;
	
	public KundeDao getKundeDao() {
		return kundeDao;
	}

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	/**
	 * returns a single Kunde by its primary db key
	 * @param id reference to customer id
	 * @return a single Kunde (customer)
	 */
	public Kunde findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Kunde) template.get(Kunde.class, id);
	}
	
	/**
	 * returns the last inserted Customer in the customer table
	 * @return last inserted customer
	 */
	public String lastInsert() {
		HibernateTemplate template = getHibernateTemplate();
		
		SessionFactory hibernateSessionFactory = template.getSessionFactory();
	
		Session sess = hibernateSessionFactory.getCurrentSession();
		SQLQuery sqlQuery = sess.createSQLQuery("SELECT max( pk_kunde )FROM tbl_kunde");
		String return_id;
		return_id = sqlQuery.getQueryString();
		return return_id;
		
	}

	/**
	 * Returns all customers from the database.
	 * @return a list of Kunde
	 * @see Kunde
	 */
	@SuppressWarnings("unchecked")
	public List<Kunde> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		

//		List boote = sqlQuery.list();
		
//		Criteria crit = sess.createCriteria(Boot.class);
//		List boote = crit.list();
//		return boote;
		
		return template.loadAll(Kunde.class);
	}
	
	/**
	 * modifies an existing customer in the database which is selected by its primary key.
	 * @param id the primary key of the customer
	 * @param name the new name for the customer
	 * @param vorname the new vorname for the customer 
	 * @param strasse the new strasse for the customer
	 * @param plz the new plz for the customer
	 * @param ort the new ort for the customer
	 * @param tel the new telephone nr. for the customer
	 * @param email the new email for the customer
	 * @param kto the new kto nr. for the customer
	 * @param blz the new blz for the customer
	 * @param bank the new bank for the customer
	 * @see Kunde
	 */
	public void modify(int id, String name, String vorname, String strasse, String plz, String ort, String tel, String email, String kto, String blz, String bank) {
		HibernateTemplate template = getHibernateTemplate();
		Kunde kunde = (Kunde) template.get(Kunde.class, id);
		kunde.setName(name);
		kunde.setVorame(vorname);
		kunde.setStrasse(strasse);
		kunde.setPlz(plz);
		kunde.setOrt(ort);
		kunde.setTel(tel);
		kunde.setEmail(email);
		kunde.setKto(kto);
		kunde.setBlz(blz);
		kunde.setBank(bank);		
		
		template.saveOrUpdate(kunde);
	}

	/**
	 * creates a new persistent customer in the database providing all the necessary details.
	 * @param name the new name for the customer
	 * @param vorname the new vorname for the customer 
	 * @param strasse the new strasse for the customer
	 * @param plz the new plz for the customer
	 * @param ort the new ort for the customer
	 * @param tel the new telephone nr. for the customer
	 * @param email the new email for the customer
	 * @param kto the new kto nr. for the customer
	 * @param blz the new blz for the customer
	 * @param bank the new bank for the customer
	 * @see Kunde
	 */
	public void create(String name, String vorname, String strasse, String plz, String ort, String tel, String email, String kto, String blz, String bank) {
		HibernateTemplate template = getHibernateTemplate();
		Kunde kunde = new Kunde(name, vorname, strasse, plz, ort, tel, email, kto, blz, bank);
		template.persist(kunde);
	}

	
}